package edu.hawaii.duedates;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import java.util.Calendar;
import java.util.Locale;
import org.junit.Before;
import org.junit.Test;

/**
 * JUnit test class for BorrowedItem class.
 * @author John Zhou
 * @author Ka Hung Phillip Lau
 *
 */
public class TestBorrowedItem {
  private BorrowedItem book;
  private BorrowedItem longBook;
  
  /**
   * Sets up the BorrowedItem object.
   */
  @Before
  public void setUp () {
    Calendar cal = Calendar.getInstance(Locale.US);
    Calendar currentTime = Calendar.getInstance(Locale.US);
    cal.set(Calendar.MONTH, 0);
    cal.set(Calendar.DAY_OF_MONTH, 15);
    cal.set(Calendar.YEAR, 2008);
    cal.set(Calendar.HOUR_OF_DAY, 23);
    cal.set(Calendar.MINUTE, 59);
    cal.set(Calendar.SECOND, 59);
    cal.set(Calendar.MILLISECOND, 0);
    book = new BorrowedItem("Cat in the Hat", cal, "Book", "UH Manoa");      
    longBook = new BorrowedItem("Some insanely long title that no one would ever want to read",
        currentTime, "Video", "Hawaii State");
  }
  
  /**
   * Tests all the get methods (getDueDate, getTitle, getLender, getType).
   */
  @Test
  public void testGetMethods () {
    int month = book.getDueDate().get(Calendar.MONTH);
    int day = book.getDueDate().get(Calendar.DATE);
    int year = book.getDueDate().get(Calendar.YEAR);
    assertEquals("Month of BorrowedItem", 0, month);
    assertEquals("Day of BorrowedItem", 15, day);
    assertEquals("Year of BorrowedItem", 2008, year);
    
    assertEquals("Testing getTitle", "Cat in the Hat", book.getTitle());
    assertEquals("Testing getLender", "UH Manoa", book.getLender());
    assertEquals("Testing getType", "Book", book.getType());    
  }
  
  /**
   * Tests the toString method of BorrowedItem.  Due to the long length
   * of the string with white spaces for formatting purposes, the string will only be 
   * tested if it contains a field.
   */
  @Test
  public void testToString () {
    assertTrue("Testing toString contains Title", book.toString().contains("Cat in the Hat"));
    assertTrue("Testing toString contains Date", book.toString().startsWith("1-15-2008"));
    assertTrue("Testing toString contains Lender", book.toString().contains("UH Manoa"));
    assertTrue("Testing toString contains Type", book.toString().contains("Book"));
    assertTrue("Testing getTitle long titles", longBook.getTitle().length() > 50);
    assertTrue("Testing toString long titles", longBook.toString().length() > 50);
  }
  
  /**
   * Tests the printDueDate method of BorrowedItem.
   */
  @Test
  public void testPrintDueDate () {
    assertEquals("Testing printDueDate", book.printDueDate(), "1-15-2008");
  }
  
  /**
   * Tests the compareTo method of BorrowedItem.
   */
  @Test
  public void testCompareTo () {
    assertSame("compareTo with same date", book.compareTo(book), 0);
    assertTrue("compareTo with diff date", book.compareTo(longBook) < 0);
    assertTrue("compareTo with diff date", longBook.compareTo(book) > 0);
  }
  
  /**
   * Tests boolean equals method of BorrowedItem.
   */
  @Test
  public void testEquals() {
    String Book = "Book1";
    String Name = "Cat in the Hat1";
    String what = "What1";
    String school = "KCC";
    Calendar currentTime = Calendar.getInstance(Locale.US);
    book = new BorrowedItem(Name, currentTime, Book, school);
    assertFalse("Not Equal BorrowedItem object", book.equals(what));
  }
  
  /**
   * Tests int hashCode method.
   */
  @Test
  public void testHashCode() {
    int num = book.hashCode();
    assertEquals("Test hashCode", 1, num);
  }
}
